package com.bjsxt.controller;

import com.alibaba.fastjson.JSON;
import com.bjsxt.pojo.Dept;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.DeptInsertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DeptInsertController {
    @Autowired
    private DeptInsertService deptInsertService;

    @RequestMapping("/showIndex")
    public String showIndex(Model model){
        model.addAttribute("list",deptInsertService.showDept());
        return "index";
    }

    @RequestMapping("/insert")
    public String insert(Emp emp){
        int result = deptInsertService.insert(emp);
        if(result==1){
            return "success";
        }
        return "fail";
    }
    @RequestMapping("/showAll")
    public String showAll(Model model){
        List<Emp> list1 = deptInsertService.selectAll();
        for (Emp emp:list1) {
            Dept dept = deptInsertService.selectBydeptId(emp.getDeptId());
            emp.setDeptname(dept.getName());
        }
        model.addAttribute("list",list1);
        System.out.println("123");
        return "show";
    }
}
